package com.education.online.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.utils.ApiReturnObject;
import com.education.online.pojo.OCatalogMenu;
import com.education.online.pojo.OFreeClass;
import com.education.online.pojo.OTestPaper;
import com.education.online.service.OCatalogMenuService;
import com.education.online.service.OTestPaperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 职业技能测试表
 *
 * @date 2020-06-04 15:17:49
 */
@Api(tags = {"PC-职业技能测试表"})
@RestController
@RequestMapping("/online/otestpaper")
public class OTestPaperController {

    @Autowired
    private OTestPaperService oTestPaperService;

    @Autowired
    private OCatalogMenuService oCatalogMenuService;

    @ApiOperation(value = "我的测试", httpMethod = "GET")
    @GetMapping("/myPaper")
    public ApiReturnObject myPaper(Integer userID) {
        List<OTestPaper> paperList = oTestPaperService.selectOTestPaperByUserId(userID);


        return new ApiReturnObject("200","seccess",paperList);
    }

    @ApiOperation(value = "搜索接口", httpMethod = "GET")
    @GetMapping("/search")
    public ApiReturnObject search(String name) {
        List<OTestPaper> testPaperList = oTestPaperService.selectOTestPaperByLikeName(name);
        return new ApiReturnObject("200","success",testPaperList.size(),testPaperList);
    }


    @ApiOperation(value = "查询全部试卷", httpMethod = "GET")
    @GetMapping("/selectAll")
    public ApiReturnObject selectAll(Integer paperId, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum,pageSize);
        }
        OTestPaper oTestPaper = new OTestPaper();
        oTestPaper.setId(paperId);
        List<OTestPaper> testPaperList = oTestPaperService.selectOTestPaperList(oTestPaper);
        PageInfo<OTestPaper> pageInfo = new PageInfo<>(testPaperList);
        return new ApiReturnObject("200", "success", pageInfo);
    }


    @ApiOperation(value = "筛选接口", httpMethod = "GET")
    @GetMapping("/screening")
    public ApiReturnObject screening(Integer menuId) {
        List<OCatalogMenu> oCatalogMenuList = oCatalogMenuService.selectOCatalogMenuByParentId(menuId);
        //判断该目录是否是一级目录 未查询到  说明是二级目录  否则是一级
        if (oCatalogMenuList.size() == 0){
            //查询二级目录下的所有课程
            List<Integer> list = new ArrayList<>();
            list.add(menuId);
            List<OTestPaper> oTestPaper = oTestPaperService.selectOFreeClassByMenuIds(list);
            return new ApiReturnObject("200", "success", oTestPaper);
        }else {
            //查询一级目录下的所有课程
            List<Integer> arrayList = new ArrayList<>();
            for (OCatalogMenu oCatalogMenu : oCatalogMenuList) {
                arrayList.add(oCatalogMenu.getId());
            }
            List<OTestPaper> oTestPaper = oTestPaperService.selectOFreeClassByMenuIds(arrayList);
            return new ApiReturnObject("200", "success", oTestPaper);
        }
    }

}
